package electricity;

import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * 计算电价
 *
 * @author liujiamiao
 * @email 2544296058@qq.com
 * @date 2020-11-09 19:53
 */
@Data
public class electrovalence {
/*
17:00:00
20:00:00
9

12:00:00
20:00:00
8

16:30:00
20:30:00
8
 */
    public static void main(String[] args) {
        //0-8低谷区 0.8元/度 -- 00:00:00
        //8-18低谷区 1.2元/度
        //18-00低谷区 1元/度
        List<Integer> list = new ArrayList<Integer>();
        String[] timeStr = new String[]{"00:00:00", "07:59:59", "17:59:59","23:59:59"};
        //每度电多少钱
        float[] money = new float[]{0.8f,1.2f,1.0f};

        //把字符串转为数字
        for(String time : timeStr){
            list.add(convertingInteger(time));
        }
        System.out.println("不同电价区域"+list);

        //输入使用的时间和电的度数
        Scanner scanner = new Scanner(System.in);
        Integer start = convertingInteger(scanner.next());
        Integer end = convertingInteger(scanner.next());
        int num = scanner.nextInt();
        System.out.println("使用电量的时间区域："+convertingString(start)+" - "+convertingString(end));

        Integer sum = end - start;
        System.out.println("使用电量时长："+convertingString(sum));

        System.out.println("使用电量为："+num);

        int i = 1;
        //每个时间段使用的时间
        Integer[] useTime = new Integer[money.length];
        for(;i<list.size();i++){
            if(start<list.get(i)){
                if (end<list.get(i)){
                    useTime[i-1] = end-start;
                    break;
                }else{
                    useTime[i-1] = list.get(i)-start+1;
                    start = list.get(i)+1;
                }
            }
        }

        System.out.println("每个区域使用的时间"+Arrays.toString(useTime));
        System.out.println("价格如下：");
        BigDecimal sumMoney = new BigDecimal(0);
        for(int j = 0;j<useTime.length;j++){
            if(useTime[j]!=null){
                //useTime[j] 该区域使用的时间
                //num 使用的总度数
                //money[j] 该区域的价格
                //sum 使用总时间
                //公式：使用的时间 / 总时间 * 使用的电量 * 该区域电价
                BigDecimal decimal = new BigDecimal(useTime[j]).multiply(new BigDecimal(num)).multiply(new BigDecimal(money[j])).divide(new BigDecimal(sum));
                String m = show(decimal);
                System.out.println(m);
                sumMoney = sumMoney.add(new BigDecimal(m));
            }
        }
        System.out.println("总电价为："+sumMoney);

    }

    /**
     * 格式化价格
     * @param decimal
     * @return
     */
    public static String show(BigDecimal decimal){
        int index = 0;
        if((index = decimal.toString().indexOf("."))!=-1){
            if(index+3 < decimal.toString().length()){
                return decimal.toString().substring(0,index+3);
            }

        }
        return decimal.toString();
    }

    //转时间
    public static Integer convertingInteger(String time){
        Integer timeLong = 0;
        String[] times = time.split(":");
        timeLong += Integer.parseInt(times[0])*60*60;
        timeLong += Integer.parseInt(times[1])*60;
        timeLong += Integer.parseInt(times[2]);
        return timeLong;
    }

    public static String convertingString(Integer time){
        StringBuilder sb = new StringBuilder();
        int seconds = time%60;
        time = time/60;
        int minute = time%60;
        time = time/60;
        sb.append((time<10?"0"+time:time)+":"+(minute<10?"0"+minute:minute)+":"+(seconds<10?"0"+seconds:seconds));
        return sb.toString();
    }

}
